package fi.bluepill.server;

import java.io.*;

public class LocalFileStorage implements FileStorage {
    private File root;

    public LocalFileStorage(File root) {
        this.root = root;
    }

    public void store(String bucket, byte[] data, String uuid) {
        try {
            FileOutputStream fos = new FileOutputStream(root.getAbsolutePath() + "/" + uuid);
            fos.write(data);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public byte[] retrieve(String bucket, String uuid) {
        try {
            FileInputStream fis = new FileInputStream(root.getAbsolutePath() + "/" +  uuid);
            byte[] data = new byte[fis.available()];
            fis.read(data);
            fis.close();
            return data;
        } catch (FileNotFoundException e) {
            return null;
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }
}
